﻿var express = require('express');
var router = express.Router();
var redis = require('redis');
var mysql = require('mysql');
var config = require('../routes/config')
var db = {}

db.getRedisData = function(key,callback){
    /**
     *  访问redis-server 填充数据 后 渲染
     * */

    var RDS_PORT = '9101';		//端口号
    var RDS_HOST = '192.168.1.38';	//服务器IP
    var client = redis.createClient(RDS_PORT,RDS_HOST);

    client.on('ready',function(err){
       // console.log('ready');

    });
    client.on("error", function(error) {
        console.log(error);

    });
    client.on("connect",function() {
        client.get(key,function(err,reply){
            if (reply)
            {
                callback(reply.toString());
            }
            else
            {
                callback(0);
            }
        });

    });
};

var datas={};

router.get("/",function (req,res,next) {
    if (0 && !req.session.login)
    {
        res.render('index',{
            links:config.linkmenu,
            index:config.link.INDEX
        });
        return;
    }
    var maps=[
        {id:100		,name:"适用车型    "},
        {id:1018	,name:"配件名称  "},
        {id:1010	,name:"产地"},
        {id:1012	,name:"库存状态  "},
        {id:1013	,name:"有优惠单位"},
        {id:1017	,name:"进价  "},
        {id:1003	,name:"零件号"},
        {id:1021	,name:"物料号  "},
        {id:1016	,name:"条码号"},
        {id:1005	,name:"上次进货日期"}
       
    ];
    var heads=[];

    var date = new Date("2016-6-30 00:00:00");
    var start = new Date("2017-1-1 00:00:00");
    var now = new Date();
    var arrTable = [];
    arrTable.push({
        tablename:'datalog',
        datetime:0,
    });
    for (var ii = date.getTime(); ii <= now.getTime(); ii += 86400 * 1000)
    {
        var iii = new Date(ii);
        var nowYear = iii.getYear();
        var nowDay = (iii.getTime() - start.getTime())/(86400*1000);
        var o ={
            tablename : 'datalog_'+nowYear+'_D_'+nowDay+'',
            datetime: iii.getTime()
        };
        arrTable.push(o);

        var o = {

        };
        // o.dateKey = dateKey;
        o.num = 11111;
        var dateKey = " "+ date.Format("yyyyMMdd");
        datas[dateKey] = [];
        datas[dateKey].push(o);
        datas[dateKey].push(o);
        datas[dateKey].push(o);
        datas[dateKey].push(o);
        datas[dateKey].push(o);
        datas[dateKey].push(o);
        datas[dateKey].push(o);
        datas[dateKey].push(o);
        datas[dateKey].push(o);
        o.num = " "+ date.Format("yyyyMMdd");;
        datas[dateKey].push(o);
    }

    heads.push("日期");
    for (var itemkey in maps){
       heads.push(maps[itemkey].name);
    }

    var dataarr = [];
    for (var key in datas)
    {
        var o = {};
        o.key = key;
        o.datas = [];
        var iiii = datas[key];
        for (var index in iiii)
        {
            var ii = {};
            ii.num = iiii[index].num;
            o.datas.push(ii);
        }
        dataarr.push(o);
       
      
    }
    console.log(JSON.stringify(datas));
    res.render('gameplay', {
        title: '角色统计' ,
        links:config.linkmenu,
        datas:dataarr,
        heads:heads,
        index:config.link.INDEX,
        login:req.session.login
    });
    /*
    var index = 0;
    config.aync_each_arr(arrTable,function(dayTable,nextdayTable){
        var tableName = dayTable.tablename;
        var date = new Date(parseInt(dayTable.datetime));
        console.log(tableName +" " +dayTable.datetime);
        var dateKey =" "+ date.Format("yyyyMMdd");
        if (datas[dateKey] || index + 1 == arrTable.length)
        {
            nextdayTable();
        }
        else
        {
            datas[dateKey] = [];
            config.aync_each_arr(maps,function(item,next){
                var mapkey = 390842023936 + item.id;
                var key = tableName+":["+mapkey+"]:value"
               db.getRedisData(key,function (value)
                {
                    var mapid = item.id ;
                    var o = {

                    };
                    // o.dateKey = dateKey;
                    o.num = value;

                    datas[dateKey].push(o);
                   // console.log("key 值为" + value + " " + dateKey);
                    next();
                });
                next();
            },function(){
                index++;
                nextdayTable();
            });
        }

    },function(){
        
    });
    */
});
module.exports = router;